package com.example.im.monitor

import android.app.Activity
import android.support.v7.app.AppCompatActivity
import android.view.View
import cc.vv.lkimcomponent.lkim.listener.LKIMConnectionListener
import cc.vv.lklibrary.log.LogOperate
import com.example.BaseNewApplication
import com.example.baselibrary.R

import com.example.mvp.im.observr.NetObservable
import com.example.mvp.view.LKDialog
import java.lang.ref.WeakReference
import java.lang.reflect.InvocationTargetException


/**
 * 连接状态
 * @author  chaohaipeng  on 2018/7/23.
 */
class ConnectionMonitor : LKIMConnectionListener {
    private var mActivity: WeakReference<Activity>? = null
    private object mHolder {
        val instance = ConnectionMonitor()
    }
    companion object {
        fun getInstance(): ConnectionMonitor {

            return mHolder.instance
        }
    }

    /**
     * 设置当前显示的Activity
     *
     * @param activity
     */
    fun setContext(activity: Activity) {
        mActivity = WeakReference(activity)

    }
    /**
     * 帐号被移出
     */
    override fun onRemoved() {
    }

    override fun onConnected() {
        LogOperate.e("链接状态")
        NetObservable.getInstance().onNewMessage(1)
    }
    /**
     * 连接中断回调
     *
     * @param errorCode
     */
    override fun onDisconnected(errorCode: Int) {
        LogOperate.e("链接状态中断"+errorCode)

    }

    /**
     * 获取当前运行的Activity
     */
    fun getActivity(): AppCompatActivity? {
        var activityThreadClass: Class<*>? = null
        try {
            activityThreadClass = Class.forName("android.app.ActivityThread")
            val activityThread = activityThreadClass!!.getMethod("currentActivityThread").invoke(null)
            val activitiesField = activityThreadClass.getDeclaredField("mActivities")
            activitiesField.isAccessible = true
            val activities = activitiesField.get(activityThread) as Map<*, *>
            for (activityRecord in activities.values) {
                val activityRecordClass:Class<*>? =activityRecord!!::class.java
                val pausedField = activityRecordClass?.getDeclaredField("paused")
                pausedField?.isAccessible = true
                val boolean = pausedField?.getBoolean(activityRecord)
                if (boolean!=null&&!boolean) {
                    val activityField = activityRecordClass?.getDeclaredField("activity")
                    activityField?.isAccessible = true
                    return activityField?.get(activityRecord) as AppCompatActivity
                }
            }
        } catch (e: ClassNotFoundException) {
            e.printStackTrace()
        } catch (e: NoSuchMethodException) {
            e.printStackTrace()
        } catch (e: IllegalAccessException) {
            e.printStackTrace()
        } catch (e: InvocationTargetException) {
            e.printStackTrace()
        } catch (e: NoSuchFieldException) {
            e.printStackTrace()
        }

        return null
    }

        /**
     * 异地登录回调
     */
    override fun onAnotherPlace() {
        LogOperate.e("链接状态中断"+getActivity())


        try {
            val activity = getActivity()
            BaseNewApplication.getInstance()?.mHandler?.post(object :Runnable{
                override fun run() {
                    if (activity != null) {
                        val dialog = LKDialog(activity)
                        dialog.setMessage(R.string.str_another_place_login)
                        dialog .setRightButton(R.string.string_confirm, View.OnClickListener {
                            dialog.dismiss()
                            BaseNewApplication.getInstance()?.loginOut(BaseNewApplication.getApplication())
                        })
                        dialog.show()
                    }
                }
            })

        } catch (e: Exception) {
            e.printStackTrace()
        }


    }

    /**
     * 没有网络回调
     */
    override fun noNetWork() {
        NetObservable.getInstance().onNewMessage(2)
    }




}